package tmc.app.results;

import tmc.app.data.GlobalVariables;
import tmc.app.data.ProcessData;
import tmc.app.login.ActivityLogin;
import tmc.app.luckydraw.ActivityLuckyDraw;
import tmc.app.luckydraw.R;
import tmc.app.luckydraw.R.layout;
import tmc.app.luckydraw.R.menu;
import tmc.app.main.MainActivity;
import android.os.Bundle;
import android.provider.Settings.Global;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ActivityResult extends Activity {

    private TextView txt_result;
	private ImageView txt_title;
	private ImageView img_play;
	private String[] plantArray;
	private Activity mActivity;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        mActivity = this;
		overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        initLayout();
        initEvents();
//        setTextWinDesc("bia saporo", GlobalVariables.lanthu);
        checkResult();
        setEmptyCount();
    }

	public void resetData()
	{
		GlobalVariables.result = null;
		GlobalVariables.hoten = null;
		GlobalVariables.phone = null;
		GlobalVariables.count = 0;
		GlobalVariables.lanthu = 0;
	}
	
	public void checkResult()
	{
		if(GlobalVariables.result.equals("0"))
		{
	        setTextLoseDesc(GlobalVariables.lanthu);
	        txt_title.setBackgroundResource(R.drawable.txt_close);
		}else{
	        txt_title.setBackgroundResource(R.drawable.txt_win);
			setTextWinDesc(plantArray[Integer.parseInt(GlobalVariables.result)-1], GlobalVariables.lanthu);
		}
	}
	
	public void onBackPressed() {};

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    public void initLayout()
    {
    	plantArray = getResources().getStringArray(
				R.array.list_giaithuong);
    	
    	txt_title = (ImageView)findViewById(R.id.txt_title);
    	txt_result = (TextView)findViewById(R.id.txt_result);
    	img_play = (ImageView)findViewById(R.id.img_play);
    }
    
    public void initEvents()
    {
    	img_play.setOnClickListener(onClickListener);
    }
    
    public OnClickListener onClickListener = new OnClickListener()
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			ProcessData.ringPhone(mActivity);
			if(GlobalVariables.lanthu != 0)
			{
				moveToPlay();
			}else{
				resetData();
				moveToHome();
			}
		}
    	
    };
    
    public void setEmptyCount()
    {
    	if(GlobalVariables.lanthu != 0)
    	{
        	img_play.setBackgroundResource(R.drawable.btn_quaytiep);
    	}else{
        	img_play.setBackgroundResource(R.drawable.btn_home_fail);
    	}
    }
    
    public void setTextWinDesc(String txt,int count)
    {
    	txt_result.setText(getResources().getString(R.string.txt_win1)+" "+txt+" "+getResources().getString(R.string.txt_win2)+"\n"+getResources().getString(R.string.txt_numberquay1)+ " "+String.valueOf(count)+" "+getResources().getString(R.string.txt_numberquay2));
    }
    
    public void setTextLoseDesc(int count)
    {
    	txt_result.setText(getResources().getString(R.string.txt_lose1)+"\n"+getResources().getString(R.string.txt_numberquay1)+ " "+String.valueOf(count)+" "+getResources().getString(R.string.txt_numberquay2));
    }
    
    public void moveToPlay()
    {
    	Intent intent = new Intent(this, ActivityLuckyDraw.class);
    	startActivity(intent);
		overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    	finish();
    }
    
    public void moveToHome()
    {
    	Intent intent = new Intent(this, MainActivity.class);
    	startActivity(intent);
		overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    	finish();
    }
}
